package com.shop.loan.handler;//package com.shop.loan.handler;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.shop.loan.mapper.LoanOrderAuthByMybatisMapper;
//import com.shop.loan.mapper.LoanOrderByMybatisMapper;
//import com.shop.loan.model.LoanOrderAuthLog;
//import com.shop.loan.model.LoanOrderSignLog;
//import com.shop.loan.model.entity.LoanOrderAuthByMybatis;
//import com.shop.loan.model.entity.LoanOrderByMybatis;
//import com.shop.loan.model.entity.NewMsgType;
//import com.shop.loan.model.entity.SettingByMybatis;
//import com.shop.loan.model.enums.OrderStatus;
//import com.shop.loan.model.enums.ServiceType;
//import com.shop.loan.model.vo.LoanOrderAuthVo;
//import com.shop.loan.redis.RedisCache;
//import com.shop.loan.redis.RedisLockUtil;
//import com.shop.loan.redis.RedisUtil;
//import com.shop.loan.service.EruptUserDataService;
//import com.shop.loan.service.MsgService;
//import com.shop.loan.service.SettingService;
//import com.shop.loan.util.LoanOrderUtil;
//import com.shop.loan.util.Util;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import redis.clients.jedis.Jedis;
//import xyz.erupt.annotation.fun.OperationHandler;
//import xyz.erupt.core.service.EruptService;
//import xyz.erupt.upms.service.EruptUserService;
//
//import javax.annotation.Resource;
//import java.util.*;
//
//@Slf4j
//@Service
//public class OrderAuthHandler implements OperationHandler<Object, LoanOrderAuthVo> {
//
//    @Resource
//    RedisUtil redisUtil;
//
//    @Resource
//    RedisLockUtil redisLockUtil;
//
//    @Resource
//    LoanOrderByMybatisMapper loanOrderByMybatisMapper;
//
//    @Resource
//    MsgService msgService;
//
//    @Resource
//    SettingService settingService;
//
//    @Resource
//    LoanOrderAuthByMybatisMapper loanOrderAuthByMybatisMapper;
//
//    @Resource
//    EruptUserService eruptUserService;
//
//    @Resource
//    EruptUserDataService eruptUserDataService;
//
//    @Transactional
//    @Override
//    public String exec(List<Object> data, LoanOrderAuthVo loanOrderAuthVo, String[] param) {
//        if (data == null || data.size() == 0) {
//            return "msg.error('未选中数据')";
//        }
//        Object item = data.get(0);
//        JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(item));
//        Long id = jsonObject.getLongValue("id");
//
//        Jedis jedis = redisUtil.getJedis();
//        String lockKey = RedisCache.orderAuthLock + id;
//        String lockValue = UUID.randomUUID().toString();
//        try {
//            if (!redisLockUtil.lock(jedis, lockKey, lockValue)) {
//                return "msg.error('请稍后重试')";
//            }
//            LoanOrderByMybatis loanOrderByMybatis = loanOrderByMybatisMapper.selectByPrimaryKey(id);
//            if (loanOrderByMybatis == null ||
//                    loanOrderByMybatis.getStatus().intValue() != OrderStatus.wait_auth.getStatus().intValue()) {
//                return "msg.error('订单状态无需审核')";
//            }
//
//            LoanOrderByMybatis updateOrder = new LoanOrderByMybatis();
//            updateOrder.setId(id);
//            if (loanOrderAuthVo.getType() == 1) {
//                //审核通过
//                if (2 != loanOrderByMybatis.getSignStatus()) {
//                    return "msg.error('订单合同未签署')";
//                }
//                SettingByMybatis settingByMybatis = settingService.getSetting();
//                updateOrder.setStatus(OrderStatus.wait_loan.getStatus());
//                if (settingByMybatis.getAutoAllocLoanService()) {
//                    /**
//                     * 自动分配放款客服
//                     */
//                    List<LoanOrderByMybatis> loanOrderByMybatisList = new ArrayList<>();
//                    loanOrderByMybatisList.add(loanOrderByMybatis);
//                    eruptUserDataService.allotServiceByOverdue(loanOrderByMybatisList, ServiceType.loan_service);
//                } else {
//                    /**
//                     * 给放款员消息提醒
//                     */
//                    msgService.newMsgByOrder(NewMsgType.loan);
//                }
//            } else {
//                updateOrder.setStatus(OrderStatus.refuse.getStatus());
//            }
//            loanOrderByMybatisMapper.updateByPrimaryKeySelective(updateOrder);
//
//            //增加一条审核记录
//            LoanOrderAuthByMybatis orderAuthLog = new LoanOrderAuthByMybatis();
//            orderAuthLog.setAuthStatus(loanOrderAuthVo.getType() == 1);
//            orderAuthLog.setOrderId(id);
//            orderAuthLog.setAuthBy(eruptUserService.getCurrentEruptUser().getName());
//            orderAuthLog.setReason(loanOrderAuthVo.getReason());
//            orderAuthLog.setCreateTime(new Date());
//            orderAuthLog.setOrderStatus(OrderStatus.wait_loan.getStatus());
//            loanOrderAuthByMybatisMapper.insertSelective(orderAuthLog);
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error(Util.GetErrorDetails(e));
//        } finally {
//            if (jedis != null) {
//                redisLockUtil.release(jedis, lockKey, lockValue);
//                jedis.close();
//            }
//        }
//        return null;
//    }
//
//
//}
